import { OneModal } from '@/components/one-ui';
import { Button, Col, message, Row, Spin } from 'antd';
import React, { useContext, useEffect, useRef } from 'react';
import { DoubleRightOutlined, DoubleLeftOutlined } from '@ant-design/icons';
import JoinRemindPeopleCenter from './JoinRemindPeopleCenter';
import JoinRemindPeopleLeft from './JoinRemindPeopleLeft';
import JoinRemindPeopleRight from './JoinRemindPeopleRight';
import { IActionEnum, RemindPeopleContext } from '../common';
import { RemindManageAPI } from '../common/type';
import {
  getFlowOrgTreeByOrgIDService,
  getUserDataList,
} from '@/services/BaseLayout';
import { getPosIDs } from '@/utils';
import '../common/index.less';
const BeRemindedPeople: React.FC<RemindManageAPI.BeremindPeople> = ({
  onChange,
  updateForm,
}) => {
  /**
   * @description: 弹窗的公共方法
   * @return {*}
   */
  const beRemindedPeople: any = useRef(null);
  const onCloseModal = () => {
    if (beRemindedPeople) {
      if (beRemindedPeople.current) {
        if (beRemindedPeople.current.onModalCancel) {
          beRemindedPeople.current.onModalCancel();
        }
      }
    }
  };
  /**
   * 判断可选数据 和已选数据大小
   * @returns
   */
  const { joinCheckBoxKeys, centreUserRows, updateDispath } = useContext(
    RemindPeopleContext,
  );
  const {
    getFlowOrgTreeRun,
    getFlowOrgList,
    getFlowOrgTreeLoading,
  } = getFlowOrgTreeByOrgIDService();
  useEffect(() => {
    let deptParams = {
      params: 0,
      flowType: 0,
      orgID: getPosIDs(),
    };
    getFlowOrgTreeRun(deptParams);
  }, []);
  /**
   * 全选和反选
   * @param onType 是否全选
   */
  const selectReverseAll = (onType: string) => {
    let checkIDs: any[] = [];
    let checkRows: any[] = [];
    if (onType == 'AllKey') {
      centreUserRows?.forEach((item: any) => {
        checkIDs?.push(item?.id);
        checkRows?.push({ ...item });
      });
      updateDispath({
        type: IActionEnum.CENTRE_CHECKBOX_KEYS,
        payload: checkIDs,
      });
      updateDispath({
        type: IActionEnum.CENTRE_CHECKBOX_ROWS,
        payload: checkRows,
      });
    }
    if (onType == 'delAllKey') {
      updateDispath({
        type: IActionEnum.CENTRE_CHECKBOX_KEYS,
        payload: [],
      });
      updateDispath({
        type: IActionEnum.CENTRE_CHECKBOX_ROWS,
        payload: [],
      });
    }
  };
  return (
    <OneModal
      title={'选择提醒人员'}
      ref={beRemindedPeople}
      open={true}
      transitionName=""
      confirmLoading={getFlowOrgTreeLoading}
      width={document.body.clientWidth * 0.65}
      bodyStyle={{
        padding: '5px 10px',
      }}
      onCancel={() => {
        onChange();
      }}
      onOk={() => {
        if (joinCheckBoxKeys?.length > 0) {
          updateForm?.setFieldsValue({
            joinIdsText: `已选择 ${joinCheckBoxKeys?.length} 位提醒人员`,
          });
        } else {
          message.warning('请至少选择一位提醒人员！');
          return;
        }
        onCloseModal();
      }}
    >
      <Spin spinning={getFlowOrgTreeLoading}>
        <Row gutter={[16, 16]}>
          {/* 第一列 */}
          <JoinRemindPeopleLeft treeDept={getFlowOrgList} />
          <JoinRemindPeopleCenter />
          <Col span={2}>
            <div
              style={{
                padding: '210px 15px',
                display: 'flex',
                flexDirection: 'column',
                alignItems: 'center',
                justifyContent: 'center',
                height: document.body.clientHeight * 0.4,
              }}
            >
              <Button
                style={{ marginBottom: 10 }}
                type={true ? 'dashed' : 'primary'}
                onClick={() => {
                  selectReverseAll('AllKey');
                }}
              >
                <DoubleRightOutlined />
              </Button>
              <Button
                type={false ? 'primary' : 'dashed'}
                onClick={() => {
                  selectReverseAll('delAllKey');
                }}
              >
                <DoubleLeftOutlined />
              </Button>
            </div>
          </Col>
          <JoinRemindPeopleRight></JoinRemindPeopleRight>
        </Row>
      </Spin>
    </OneModal>
  );
};

export default BeRemindedPeople;
